{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "X= np.array([[1.2, 1.5, 1.8],\n",
    "            [1.3, 1.4, 1.9],\n",
    "            [1.1, 1.6, 1.7]])\n",
    "y = np. array([5, 10, 9]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[37.2, 37.599999999999994, 36.8]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 循环计算  +测试性能？\n",
    "%time\n",
    "Money = []\n",
    "for (x1,x2,x3) in X:\n",
    "    money = x1*y[0]+x2*y[1]+x3*y[2]\n",
    "    Money.append(money)\n",
    "Money  # 结果为啥那么多小数？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "totally cost 0.0\n"
     ]
    }
   ],
   "source": [
    "# 矩阵点乘  +测试性能？\n",
    "time_start=time.time()\n",
    "\n",
    "Money1 = np.dot(X,y)\n",
    "Money1\n",
    "time_end=time.time()\n",
    "print('totally cost',time_end-time_start)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(1)\n",
    "X = np.random.randint(1,10,size = 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "X1 = X.reshape(-1,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 1],\n",
       "       [1, 1, 0],\n",
       "       [8, 7, 0],\n",
       "       [5, 6, 0],\n",
       "       [5, 3, 1],\n",
       "       [8, 8, 0],\n",
       "       [8, 1, 2],\n",
       "       [8, 7, 0],\n",
       "       [1, 2, 2],\n",
       "       [9, 4, 2]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x3 = X1[:,2]\n",
    "for i in range(len(x3)):\n",
    "    if x3[i] <= 3:\n",
    "        x3[i] = 0\n",
    "    elif x3[i] > 3 and x3[i] <= 6:\n",
    "        x3[i] = 1\n",
    "    elif x3[i] > 6 :\n",
    "        x3[i] = 2\n",
    "X1[:,2] = x3\n",
    "X1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "X_train = \n",
      " [[6 9]\n",
      " [1 1]\n",
      " [8 7]\n",
      " [5 6]\n",
      " [5 3]\n",
      " [8 8]\n",
      " [8 1]\n",
      " [8 7]\n",
      " [1 2]\n",
      " [9 4]]\n",
      "y_train =  [1 0 0 0 1 0 2 0 2 2]\n"
     ]
    }
   ],
   "source": [
    "X_train = X1[:,:2]\n",
    "y_train = X1[:,-1]\n",
    "print(\"X_train = \\n\",X_train)\n",
    "print(\"y_train = \",y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1],\n",
       "       [8, 7],\n",
       "       [5, 6],\n",
       "       [8, 8],\n",
       "       [8, 7]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分类 0\n",
    "X_train[y_train == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9],\n",
       "       [5, 3]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分类 1\n",
    "X_train[y_train == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 1],\n",
       "       [1, 2],\n",
       "       [9, 4]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分类 2\n",
    "X_train[y_train == 2]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
